package com.yuandian.management.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandian.api.management.entity.ConfWeekdays;
import com.yuandian.management.mapper.ConfWeekdaysMapper;
import com.yuandian.management.service.ConfWeekdaysService;
import com.yuandian.management.utils.Utils;
import com.yuandian.management.vo.Weekday;
import com.yuandian.management.vo.WeekdayParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 周末及节假日表-Service实现
 * @author zfsn
 * @date 2023-7-28
 */
@Service
public class ConfWeekdaysServiceImpl extends ServiceImpl<ConfWeekdaysMapper, ConfWeekdays>
        implements ConfWeekdaysService {

	@Autowired
	private ConfWeekdaysMapper mapper;

	@Override
	public Boolean addOneYearDays(WeekdayParams params) {
		try {
			List<Weekday> weekdayList= Utils.getYearOfDays(params.getStartTime(),params.getEndTime());

			List<ConfWeekdays> confWeekdaysList = weekdayList.stream().map(weekday -> {
				ConfWeekdays confWeekdays = new ConfWeekdays();
				confWeekdays.setDay(weekday.getDay());
				confWeekdays.setDayWeekStr(weekday.getDayWeekStr());
				confWeekdays.setDayWeek(weekday.getDayWeek());
				confWeekdays.setType(weekday.getType());
				return confWeekdays;
			}).collect(Collectors.toList());

			return this.saveBatch(confWeekdaysList);
		} catch (ParseException e) {
			log.error(e.getMessage(), e);
		}

		return false;
	}

	@Override
	public Integer getCountByTimeRange(WeekdayParams params) {
		return mapper.getCountByTimeRange(params);
	}

	@Override
	public Integer updateTypeByDay(WeekdayParams params) {
		return mapper.updateTypeByDay(params);
	}

	@Override
	public Integer deleteByTimeRange(WeekdayParams params) {
		return mapper.deleteByTimeRange(params);
	}

	@Override
	public Page<Weekday> queryPageWeekday(WeekdayParams params) {
		Page<Weekday> page = new Page<>(params.getPageNum(), params.getPageSize());
		Page<Weekday> pageResult = mapper.queryPageWeekday(page, params);
		return pageResult;
	}

	@Override
	public List<Weekday> queryNoPageWeekday(WeekdayParams params) {
		return mapper.listWeekday(params);
	}
}